//基础配置
import APP from '@/config/app.js'
//ajax请求
import AJAX from '@/plugin/u-ajax/js_sdk/index.js'
//公共方法
import methodBase from '@/common/base.js';

  function baseRequest(url, method, data) {
	//判断网络
	var ifnetwork = methodBase.ifnetwork();
	//无网络
	if (!ifnetwork) {
		uni.showModal({
			title: '网络连接失败',
			showCancel:false,
			content: '网络连接出问题了,请检查您的网络连接',
			success: function (res) {
				console.log('网络连接失败');
			}
		});
		return false;
	} else {
		//有网络
	 return new Promise((resolve,reject) => {
		uni.showToast({
			image:'/static/images/icon.png',
			title: '加载中',
			mask:true,
		});
		//加入分页
		data['page_count'] = APP.LIMIT;
		if(!data['page']){
			data['page'] = 1;	
		}
	     AJAX({
			method: method,
			url: APP.HTTP_REQUEST_URL + url,
			data: data,
			header: APP.HEADE,
		}).then(res => {
				 uni.hideToast();
				resolve(res);
			}).catch(err => {
				 uni.hideToast();
				resolve(err);
			})
		 })
	}
}
const request = {};

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
	request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
});


export default {
	request
}